import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from './../views/index'
import Register from './../views/register'
import Login from './../views/login'
import NotFound from './../views/404'
import Home from '../views/Home'
import InfoShow from '../views/InfoShow'
import FundList from '../views/FoundList'

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '*',
    name: '/404',
    component: NotFound
  },
  {
    path: '/index',
    name: '/index',
    component: Index,
    children: [
      {
        path: '',
        component: Home
      },
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/infoShow',
        name: 'infoShow',
        component: InfoShow
      },
      {
        path: '/fundList',
        name: 'fundList',
        component: FundList
      }
    ]
  },
  {
    path: '/register',
    name: '/register',
    component: Register
  },
  {
    path: '/login',
    name: '/login',
    component: Login
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.NODE_ENV === 'production' ? '/wuhen_client/' : '/',
  routes
});

//路由守卫（在未登录状态之前只能访问到登录和注册页面）
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.wuhenToken ? true : false;
  if(to.path == '/login' || to.path == '/register'){
    next();
  }else{
    isLogin ? next() : next('/login');
  }
});

export default router
